//
//  RHDeviceCircleView.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/28.
//

#import "RHDeviceCircleView.h"

#define degreesToRadinas(x) (M_PI * (x)/180.0)

@interface RHDeviceCircleView()

@property (nonatomic ,strong) QMUILabel *socLab;

@property (nonatomic ,strong) QMUILabel *sohLab;

@end

@implementation RHDeviceCircleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    UIView *circleBg = [[UIView alloc] init];
    [circleBg setBackgroundColor:UIColor.whiteColor];
    [self addSubview:circleBg];
    
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);;  // 设置显示的frame
//    gradientLayer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];  // 设置渐变颜色
//    //    gradientLayer.locations = @[@0.0, @0.2, @0.5];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
//    gradientLayer.startPoint = CGPointMake(0, 0);   //
//    gradientLayer.endPoint = CGPointMake(1, 0);     //
//    [circleBg.layer addSublayer:gradientLayer];
    
    
    UIImageView *circleLineV = [[UIImageView alloc] init];
    [circleLineV setImage:[UIImage imageNamed:@"gplot_circle_line"]];
    [circleBg addSubview:circleLineV];
    
    UIImageView *batteryV = [[UIImageView alloc] init];
    [batteryV setImage:[UIImage imageNamed:@"gplot_battery_icon"]];
    [circleBg addSubview:batteryV];
    
    
    NSArray *colors = @[@"#DCDCDC",@"#3ECE80",@"#ff0000"];
    NSArray *numbers = @[@20,@0,@0];
    NSArray *widths = @[@200,@175,@139];
    NSArray *lineWidths = @[@6.0,@15.0,@20.0];
    __block  CGFloat lastAngle;
    //CGFloat width = 200;
    [colors enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger num = [numbers[idx] intValue];
        CGFloat width = [widths[idx] floatValue];
        
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);;  // 设置显示的frame
        if(idx == 1){
            gradientLayer.colors = @[(id)CFMainColor.CGColor,(id)CFMainColor.CGColor];  // 设置渐变颜色
              gradientLayer.locations = @[@1, @1];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
    //        gradientLayer.startPoint = CGPointMake(0, 0);   //
    //        gradientLayer.endPoint = CGPointMake(1, 0);     //
        }else{
            gradientLayer.colors = @[(id)[UIColor whiteColor].CGColor,(id)CFMainColor.CGColor];  // 设置渐变颜色
              //gradientLayer.locations = @[@0,@1.0];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
            gradientLayer.startPoint = CGPointMake(0, 1);   //
            gradientLayer.endPoint = CGPointMake(1, 0);     //
        }
        [circleBg.layer addSublayer:gradientLayer];
        
        //if (num > 0) {
            //CGFloat ratio = [numbers[idx] floatValue]/20;
            CGFloat ratio = 1;
            CGFloat angle = degreesToRadinas(ratio*(360-45))+lastAngle;
            UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width/2, width/2) radius:width/2 startAngle:M_PI/2+M_PI/8+M_PI/36+lastAngle endAngle:M_PI/2+M_PI/8+angle-M_PI/36 clockwise:YES];
            CAShapeLayer *bgLayer = [CAShapeLayer layer];
            bgLayer.frame = CGRectMake(0, 0, width, width);
            bgLayer.backgroundColor = UIColor.clearColor.CGColor;
            bgLayer.fillColor = [UIColor clearColor].CGColor;//填充色 -  透明
            bgLayer.lineWidth = [lineWidths[idx] floatValue];
            bgLayer.strokeColor = FontColor(obj).CGColor;//线条颜色
            bgLayer.strokeStart = 0;//起始点
            bgLayer.strokeEnd = 1;//终点
            bgLayer.position = CGPointMake((SCREEN_WIDTH-30)/2, 110);
            bgLayer.lineCap = kCALineCapRound;//让线两端是圆滑的状态
            bgLayer.path = circlePath.CGPath;//这里就是把背景的路径设为之前贝塞尔曲线的那个路径
            //[circleBg.layer addSublayer:bgLayer];
            gradientLayer.mask = bgLayer;
            //lastAngle = angle;
        //}
    }];
    
    


    NSArray *titles =@[@"SoC:",@"SoH:"];
    NSMutableArray *array = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *lab = [[QMUILabel alloc] init];
        [lab setTextAlignment:NSTextAlignmentLeft];
        [lab setText:obj];
        [lab setTextColor:idx == 0? FontColor(@"#56C271"):FontColor(@"#999999")];
        [lab setFont:[UIFont boldSystemFontOfSize:11]];
        [circleBg addSubview:lab];
        [array addObject:lab];
        
         QMUILabel *num_lab = [[QMUILabel alloc] init];
         [num_lab setTextAlignment:NSTextAlignmentLeft];
         [num_lab setTextColor:UIColor.blackColor];
         [num_lab setFont:[UIFont boldSystemFontOfSize:11]];
         [circleBg addSubview:num_lab];
        
         [num_lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lab.mas_bottom);
            make.left.equalTo(lab);
            make.size.mas_equalTo(CGSizeMake(45, 15));
         }];
        
        [num_lab setText:@"--%"];
        
        if (idx == 0) {
            self.socLab = num_lab;
        }else{
            self.sohLab = num_lab;
        }
        
    }];
    
    [array mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(circleLineV.mas_centerX).offset(10);
        make.width.mas_equalTo(30);
    }];
    
    [array mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:15 leadSpacing:46 tailSpacing:56];
    
    [batteryV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(32, 58));
        make.right.equalTo(circleBg.mas_centerX);
        make.centerY.mas_offset(0);
    }];
    
    [circleBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    [circleLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(119, 119));
        make.center.mas_offset(0);
    }];
}



@end
